# -*- coding:utf-8 -*-
from __future__ import absolute_import, division, print_function, with_statement
import json
import re


class Registered(list):
    def __init__(self):
        super(Registered, self).__init__()

    def get_config(self, msg):
        for conf in self:
            try:
                value = json.loads(msg.value.decode())
            except Exception as e:
                print(e)
                return None

            m_app = value.get('app')
            m_content = value.get('content', None)
            uri_pattern = conf.uri_pattern
            match = True
            if m_content and uri_pattern:
                uri = m_content.get('uri', None)
                match = re.search(uri_pattern, uri)
            try:
                if conf.method.upper() == m_content.get('method').upper() \
                        and conf.app == m_app and match:
                    return conf
            except Exception as e:
                print(e)
                return None
            else:
                # print('不匹配')
                pass
